package com.bianguo.android.iocdemo.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import com.bianguo.android.injectlibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;

public class TailorView extends View implements ScaleGestureDetector.OnScaleGestureListener {

    private RectF mRectF;
    private int mWidth, mHeight;
    private int mCenterX, mCenterY;
    private Paint mPaint;
    private Matrix mMatrix;
    private ScaleGestureDetector scaleGestureDetector;
    private float lastDistance;
    private float mScale;

    public TailorView(Context context) {
        this(context, null);
    }

    public TailorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TailorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mRectF = new RectF();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(ActivityCompat.getColor(context, R.color.pink));
        mPaint.setStrokeWidth(8);
        scaleGestureDetector = new ScaleGestureDetector(context, this);
        mScale = 1.0f;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasure(widthMeasureSpec, 0), getMeasure(heightMeasureSpec, 1));
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        mRectF.left = 0;
        mRectF.top = 0;
        mRectF.right = mWidth;
        mRectF.bottom = mHeight;
    }

    private int getMeasure(int measureSpec, int measureType) {
        int result = 0;
        switch (MeasureSpec.getMode(measureSpec)) {
            case MeasureSpec.AT_MOST:
                result = 20;
                break;
            case MeasureSpec.EXACTLY:
                result = MeasureSpec.getSize(measureSpec);
                break;
        }
        return result;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = w / 2;
        mCenterY = h / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(mRectF, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                lastDistance = getPointDistance(event);
                break;
            case MotionEvent.ACTION_MOVE:
                if (2 == event.getPointerCount()) {
                    float newDistance = getPointDistance(event);
                    float scale = newDistance / lastDistance;
                    scale *= mScale;
                    setScaleX(scale);
                    setScaleY(scale);
                    mScale = scale;
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    private float getPointDistance(MotionEvent event) {
        float distance = 0f;
        if (event.getPointerCount() == 2) {
            scaleGestureDetector.onTouchEvent(event);
            float distanceX = Math.abs(event.getX(0) - event.getX(1));
            float distanceY = Math.abs(event.getY(0) - event.getY(1));
            distance = (float) Math.sqrt(distanceX * distanceX + distanceY * distanceY);
        }
        return distance;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        MLog.i("onScale : " + detector.getScaleFactor());
        return false;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        MLog.i("onScaleBegin : " + detector.getScaleFactor());
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
    }
}
